import de.ruedigermoeller.fastcast.bigtest.*;
import de.ruedigermoeller.fastcast.bigtest.ClusterClientNode;
import de.ruedigermoeller.fastcast.bigtest.DataMutatingClient;
import de.ruedigermoeller.fastcast.bigtest.HashHostNode;
import de.ruedigermoeller.fastcast.bigtest.RequestProcessorNode;
import de.ruedigermoeller.fastcast.service.fcdemon.FCDemon;
import de.ruedigermoeller.fastcast.service.fcdemon.FCStartScript;

import java.lang.Exception;
import java.lang.Integer;
import java.util.List;

/**
 * fcon -j BigTest
 * can be changed without recompile. No Generics support (janino)
 */
public class BigTest extends FCStartScript {

    public static int HTHOST = 3;
    public static int REQPROC = 0;
    public static int RQCLIENT = 0;
    public static int MUTATOR = 0;

    public void startCluster(String arg, List demons) {
        String args[] = arg.split(" ");
        try {
            int numData = HTHOST;
            int numReqProc = REQPROC;
            int numReqClient = RQCLIENT;
            int numMut = MUTATOR;
            try {
                numData = Integer.parseInt(args[0]);
            } catch (Exception e) {

            }
            try {
                numReqProc = Integer.parseInt(args[1]);
            } catch (Exception e) {

            }
            try {
                numReqClient = Integer.parseInt(args[2]);
            } catch (Exception e) {

            }
            try {
                numMut = Integer.parseInt(args[3]);
            } catch (Exception e) {

            }

            String opts = "-Xms2g -Xmx2g -XX:+UseConcMarkSweepGC -XX:-UseAdaptiveSizePolicy -XX:SurvivorRatio=8 -XX:NewRatio=2 ";

            if ( numData > 0 ) {
                System.out.println("Starting node "+TableDataQuery.class.getName());
                startNode(opts, de.ruedigermoeller.fastcast.bigtest.TableDataQuery.class, arg);
                sleep(1000);
            }
            for ( int i = 0; i < numData; i++) {
                System.out.println("Starting node "+HashHostNode.class.getName()+"-"+i);
                FCDemon.ProcessEntry en = startNode(opts,HashHostNode.class, arg);
                if ( en == null ) {
                    System.out.println("   failed");
                }
            }
            for ( int i = 0; i < numReqProc; i++) {
                System.out.println("Starting node "+RequestProcessorNode.class.getName()+"-"+i);
                FCDemon.ProcessEntry en = startNode(opts,RequestProcessorNode.class, ""+i+" "+numReqProc);
                if ( en == null ) {
                    System.out.println("   failed");
                }
            }
            if ( numReqClient > 0 || numMut > 0 ) {
                Thread.sleep(5000);
            }
            for ( int i = 0; i < numReqClient; i++) {
                System.out.println("Starting node "+ClusterClientNode.class.getName()+"-"+i);
                FCDemon.ProcessEntry en = startNode(opts,ClusterClientNode.class, arg);
                if ( en == null ) {
                    System.out.println("   failed");
                }
            }
            for ( int i = 0; i < numMut; i++) {
                System.out.println("Starting node "+DataMutatingClient.class.getName()+"-"+i);
                FCDemon.ProcessEntry en = startNode(opts,DataMutatingClient.class, arg);
                if ( en == null ) {
                    System.out.println("   failed");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}